package com.pj.wisdomlogistics.mvp.model;


import com.google.gson.Gson;
import com.pj.wisdomlogistics.BuildConfig;
import com.pj.wisdomlogistics.entity.response.BaseResponse;
import com.pj.wisdomlogistics.entity.response.MealDetailInfo;
import com.pj.wisdomlogistics.mvp.frame.MvpModel;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.PostFormBuilder;

import java.io.File;
import java.io.IOException;

import okhttp3.Response;
import rx.Observable;
import rx.Subscriber;


public class MealDetailModel extends MvpModel {

    private static final String POST_URL = BuildConfig.SERVER_HOST;

    public Observable<BaseResponse> diningApplySign(
            final String userID,
            final int diningApplyID,
            final File sign,
            final int state,
            final String comment
    ) {
        return
                Observable.create(new Observable.OnSubscribe<BaseResponse>() {
                    @Override
                    public void call(Subscriber<? super BaseResponse> subscriber) {
                        PostFormBuilder formBuilder = OkHttpUtils.post().url(POST_URL + "diningApplySign.do");

                        formBuilder.addParams("diningApplyID", diningApplyID + "");
                        formBuilder.addParams("userID", userID);

                        if (sign != null) {
                            formBuilder.addFile("sign", sign.getAbsolutePath(), sign);
                        }

                        formBuilder.addParams("state", state + "");
                        formBuilder.addParams("comment", comment);


                        try {
                            Response response = formBuilder.build().execute();
                            String str = response.body().string();
                            BaseResponse postResponse = new Gson().fromJson(str, BaseResponse.class);
                            subscriber.onNext(postResponse);
                            subscriber.onCompleted();
                        } catch (IOException e) {
                            e.printStackTrace();
                            subscriber.onError(e);
                        }
                    }
                });
    }

    public Observable<BaseResponse> diningApplySign2(
            final String userID,
            final int diningApplyID,
            final File sign,
            final int state,
            final String comment
    ) {
        return
                Observable.create(new Observable.OnSubscribe<BaseResponse>() {
                    @Override
                    public void call(Subscriber<? super BaseResponse> subscriber) {
                        PostFormBuilder formBuilder = OkHttpUtils.post().url(POST_URL + "diningApplySign2.do");

                        formBuilder.addParams("diningApplyID", diningApplyID + "");
                        formBuilder.addParams("userID", userID);

                        if (sign != null) {
                            formBuilder.addFile("sign", sign.getAbsolutePath(), sign);
                        }

                        formBuilder.addParams("state", state + "");
                        formBuilder.addParams("comment", comment);


                        try {
                            Response response = formBuilder.build().execute();
                            String str = response.body().string();
                            BaseResponse postResponse = new Gson().fromJson(str, BaseResponse.class);
                            subscriber.onNext(postResponse);
                            subscriber.onCompleted();
                        } catch (IOException e) {
                            e.printStackTrace();
                            subscriber.onError(e);
                        }
                    }
                });
    }


    public Observable<BaseResponse<MealDetailInfo>> findDiningApplyDetail(int diningApplyID) {
        return mApiService.findDiningApplyDetail(diningApplyID);
    }

    public Observable<BaseResponse> diningApplyReview(
            String userID,
            int diningApplyID,
            String state,
            String comment) {
        return mApiService.diningApplyReview(userID , diningApplyID , state , comment);
    }

    public Observable<BaseResponse> diningApplyFinish(
            String userID,
            int diningApplyID,
            String state,
            String comment ,
            String room) {
        return mApiService.diningApplyFinish(userID , diningApplyID , state , comment , room);
    }

    public Observable<BaseResponse> cancelDiningApply(
            int diningApplyID
    ) {
        return mApiService.cancelDiningApply(diningApplyID);
    }

}
